﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Employee_CreateVacancy : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        txtDateCreation.Text =  DateTime.Now.ToShortDateString();
        SetDDLTypeOfWork();
        Calendar1.VisibleDate.IsDaylightSavingTime();
    }
    public void SetDDLTypeOfWork()
    {
        try
        {
            if (ddlTypeJob.Items.Count < 2)
            {
                Vacancy V = new Vacancy();
                DataTable tb = new DataTable();
                tb = V.TypeOfWork();
                foreach (DataRow item in tb.Rows)
                {
                    ddlTypeJob.Items.Add(item["JobName"].ToString());
                }
            }
        }
        catch (Exception ex)
        {
            Console.Write(ex.Message);
        }
    }
    protected void btnClear_Click(object sender, EventArgs e)
    {
        ddlTypeJob.ClearSelection();
        txtTitle.Text = "";
        txtDecription.Text = "";
        txtDateFilfilled.Text = "";
        txtNumberofJob.Text = "";
        lblTypeofworkError.Text = "";
        lblTitleError.Text = "";
        lblDescriptError.Text = "";
        lblDateError.Text = "";
        lblNumber.Text = "";
        lblError.Text = "";
    }
    protected void btnCreate_Click(object sender, EventArgs e)
    {
        lblSuccess.Text = "";
        int numberofjob;
        if (ddlTypeJob.SelectedValue.Equals("Please choise Type of work"))
        {
            lblTypeofworkError.Text = "(*)";
            lblError.Text = "Check type of work.";
            ddlTypeJob.Focus();
        }
        else if (txtTitle.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "(*)";
            lblError.Text = "Tilte vacancy can't be empty";
        }
        else if (txtDecription.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "(*)";
            lblError.Text = "Description can't be empty.";
            txtDecription.Focus();
        }
        else if (txtNumberofJob.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "(*)";
            lblError.Text = "Number of job can't be empty.";
            txtNumberofJob.Focus();
        }
        else if (txtDateFilfilled.Text.Equals(""))
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblDateError.Text = "(*)";
            lblError.Text = "Date Fulfilled can't be empty.";
            txtDateFilfilled.Focus();
        }
        else
        {
            lblTypeofworkError.Text = "";
            lblTitleError.Text = "";
            lblDescriptError.Text = "";
            lblNumber.Text = "";
            lblDateError.Text = "";
            lblError.Text = "";
            try
            {
                numberofjob = Convert.ToInt32(txtNumberofJob.Text);
                try
                {
                    DateTime date = new DateTime();
                    date = Convert.ToDateTime(txtDateFilfilled.Text);
                    if (date > DateTime.Now)
                    {
                        Vacancy V = new Vacancy();
                        bool bol = V.InsertVacancy(txtTitle.Text, "H0001", CheckNumberTypeOfWork(), txtDecription.Text, numberofjob, DateTime.Now, Convert.ToDateTime(txtDateFilfilled.Text));
                        if (bol)
                        {
                            lblSuccess.Text = "Added new vacancy: " + txtTitle.Text;
                        }
                        else
                        {
                            lblError.Text = "Add new vacancy failed.";
                        }
                    }
                    else
                    {
                        lblDateError.Text = "(*)";
                        lblError.Text = "Date Fulfilled can't smaller Date Creation. ";
                    }
                }
                catch (Exception)
                {
                    lblDateError.Text = "(*)";
                    lblError.Text = "Check Date Fulfilled.";
                }
            }
            catch (Exception)
            {
                lblNumber.Text = "(*)";
                lblError.Text = "Number of Job is number.";
                txtNumberofJob.Focus();
            }
        }
    }
    protected void Calendar1_SelectionChanged(object sender, EventArgs e)
    {
        lblDateError.Text = "";
        lblError.Text = "";
        if (Calendar1.SelectedDate >= DateTime.Now.Date)
        {
            txtDateFilfilled.Text = Calendar1.SelectedDate.ToShortDateString();
        }
        else
        {
            lblSuccess.Text = "";
            lblDateError.Text = "(*)";
            lblError.Text = "Date Fulfilled can not smaller Date Creation";
        }
    }
    public int CheckNumberTypeOfWork()
    {
        int result = 0;
        try
        {
            Vacancy V = new Vacancy();
            DataTable tb = new DataTable();
            tb = V.TypeOfWork();
            foreach (DataRow item in tb.Rows)
            {
                if (item["JobName"].ToString().Equals(ddlTypeJob.SelectedValue))
                {
                    result = Convert.ToInt32(item["JobID"].ToString());
                }
            }
        }
        catch (Exception)
        {
        }
        return result;
    }
}